package com.github.prontera.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.github.prontera.domain.ClientVisitor;
import com.github.prontera.persistence.ClientVisitorMapper;
import com.github.prontera.persistence.CrudMapper;

/**
 * 访客
 */
@Service
public class ClientVisitorService extends CrudServiceImpl<ClientVisitor>{
    
    @Autowired
    private ClientVisitorMapper clientVisitorMapper;
    
  	@Autowired
    public ClientVisitorService(CrudMapper<ClientVisitor> mapper) {
        super(mapper);
    }
  	
  	public boolean insertVisitor(Long clientId,Long visitorId) {
  		ClientVisitor clientVisitor=new ClientVisitor();
  		clientVisitor.setClientId(clientId);
  		clientVisitor.setVisitorId(visitorId);
		try {
			clientVisitorMapper.insert(clientVisitor);
		} catch (Exception e) {
			if(e instanceof DuplicateKeyException){
				clientVisitorMapper.updateTime(clientVisitor);
			}
		}
		return true;
	}

	public List<ClientVisitor> findVisitor(ClientVisitor clientVisitor) {
		return clientVisitorMapper.findVisitor(clientVisitor);
	}

}
